package dai.ui.composition

import android.view.View
import dai.ui.UITween
import dai.ui.cloneTween

// Repeat   count <= 0 表示 无线次    count = 1 表示只执行一次
class Repeat(val count:Int,tween: UITween): UITween(){
    var firstExecute = false
    var completedCount = 0
    val srcTween: UITween = tween

    var cloneTween: UITween? = null
    override fun initialize(node: View) {
        cloneTween = srcTween.cloneTween()
        cloneTween!!.target = node
        cloneTween!!.initialize(node)
    }

    override fun execute(dt: Float) {
        val node = target!!
        if (!firstExecute) {
            initialize(node)
            firstExecute = true
        }
        cloneTween!!.execute(dt)
        if (cloneTween!!.end){
            if (cloneTween!!.nextTween == null){

                // 判断是否是无限次
                if (count <= 0){
                    initialize(node)
                }else{
                    completedCount++
                    if(completedCount >= count){
                        end = true
                    }else{
                        initialize(node)
                    }
                }
            }else{
                cloneTween = cloneTween!!.nextTween
                cloneTween!!.target = node
            }
        }
    }

    override fun clone(): UITween {
         return Repeat(count,srcTween.cloneTween())
    }
}